<template>
  <!-- 法催案件管理-个人案件池-页面 -->
  <section class="page legal-person-case-manage">
    <data-form :rules="rules" :model="model" @onSearch="refreshData" @handleResetForm="handleResetForm" :page="pageService">
      <template slot="inputs">
        <el-form-item label="订单编号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="model.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="model.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="model.loanDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="model.partnerName"></el-input>
        </el-form-item>
        <el-form-item label="机构" prop="deptId">
          <select-department v-model="model.deptId" @on-select-change="(id, code) => departemntChange(id, code)"></select-department>
        </el-form-item>
        <el-form-item label="催收员" prop="currentCollectorRealName">
          <el-input v-model="model.currentCollectorRealName"></el-input>
        </el-form-item>
        <el-form-item label="部门催收剩余天数" prop="partCollectionRemainingDays">
          <number-range v-model="model.partCollectionRemainingDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="个人催收剩余天数" prop="personalCollectionRemainingDays">
          <number-range v-model="model.personalCollectionRemainingDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="model.bankName"></el-input>
        </el-form-item>
        <el-form-item label="催收总金额" prop="overdueAmount">
          <number-range v-model="model.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="model.businessAddress"></el-input>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="model.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="model.carNo"></el-input>
        </el-form-item>
        <el-form-item label="案件来源" prop="dataFromSource">
          <el-select v-model="model.dataFromSource" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('DataFromSource')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker type="datetimerange" :editable="false" v-model="model.followInTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="案件标记" prop="caseMark">
          <el-select v-model="model.caseMark" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CaseMark')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="queryPageInfo" :sort="sortService" :page="pageService" ref="data-box" showCaseMark>
      <template slot="buttons">
        <el-button @click="advanceCirculation" v-auth="309">申请流转</el-button>
      </template>
      <template slot="append">
        <case-marker @change-color="onColorChange" :selectionList="selectionList"></case-marker>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partCollectionRemainingDays" label="部门催收剩余天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="personalCollectionRemainingDays" label="个人催收剩余天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="deptName" label="机构" show-overflow-tooltip :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="currentCollectorRealName" label="催收员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="leaveCaseFlag" label="留案标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.leaveCaseFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="leaveDueDate" label="留案到期日期" :min-width="$helper.getColumnWidth(5)" :formatter="(row) => $filter.dateFormat(row.leaveDueDate)">
        </el-table-column>
        <el-table-column prop="telAssistflag" label="电催协催标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.telAssistflag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="assistFlag" label="外访协催标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => row.assistFlag?(row.assistFlag==='DISABLED'?'否':'是'):''">
        </el-table-column>
        <el-table-column prop="riskFlag" label="风险客户标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.riskFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="loanAmount" label="贷款金额" :min-width="$helper.getColumnWidth(4)" :formatter="(row) =>  $filter.toThousands(row.loanAmount)">
        </el-table-column>
        <el-table-column prop="loanPeriod" label="贷款期数" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overdueStage" label="逾期阶段" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueAmount" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="carNo" label="车牌号" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="dataFromSource" label="案件来源" :formatter="(row) => $filter.dictConvert(row.dataFromSource,'DataFromSource')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="followInTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.followInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
      </template>
      <p slot="footer">{{`查询结果统计：列表欠款总金额 ${$filter.toThousands(totalInfo.totalAmount)}元`}}</p>
    </data-box>

    <!--申请弹框-->
    <el-dialog :title="`${applyType.name}申请`" width="800px" :visible.sync="dialog.apply" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <apply :caseIds="selectedCaseIds" :fromChannel="caseChannel" :applyType="applyType.value" @close="dialog.apply = false;refreshData()" ref="apply-circulation"></apply>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import NumberRange from "~/components/common/number-range.vue";
import { LegalCaseManageService } from "~/services/domain-service/legal-case-manage.service";
import CaseMarker from '~/components/common/case-marker.vue';
import Apply from "~/components/visit-case/apply.vue";
import { ApplyType, CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import { Getter, Mutation, namespace, State } from "vuex-class";
import { CaseInfoService } from '~/services/domain-service/case-info.service';
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";
import SelectDepartment from "~/components/business-component/select-department.vue";

const CollectionExecutionModule = namespace("collection-execution");

@Auth(302)
@Layout("workspace")
@Component({
  components: {
    NumberRange,
    CaseMarker,
    Apply,
    GetLabelName,
    SelectDepartment
  }
})
export default class LegalPersonCaseManage extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(LegalCaseManageService)
  private legallyCaseManageServiceTestD: LegalCaseManageService;
  @Dependencies(CaseInfoService)
  private caseInfoService: CaseInfoService;
  @Mutation
  private updateCollectionType;
  @Getter
  private callDepartmentData;
  @State private principalList: any;


  private model: any = {
    caseNumber: "",
    personalName: "",
    mobileNo: "",
    idCard: "",
    overdueAmount: {
      max: "",
      min: ""
    },
    loanDate: [],
    principalId: '',
    partCollectionRemainingDays: { min: '', max: '' },
    personalCollectionRemainingDays: { min: '', max: '' },
    bankName: '',
    businessAddress: '',
    boardAddress: '',
    carNo: '',
    dataFromSource: '',
    followInTime: [],
    deptId: '',
    currentCollectorRealName: '',
    partnerName: '',
    batchNumber: '',
    caseMark: ''
  };
  private rules: any = {};
  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private caseMark: any = "";
  private dialog: any = {
    apply: false
  };
  private applyType = ApplyType.turn;
  private caseChannel = CASE_CHANNEL.legal;
  private totalInfo: any = {}

  private mounted() {
    this.refreshData();
  }

  private activated() {
    this.refreshData();
  }

  private departemntChange(id, code) { }

  private paramsHandler() {
    const params = { ...this.model }
    params.overdueAmountMax = this.model.overdueAmount ? this.model.overdueAmount.max : ""
    params.overdueAmountMin = this.model.overdueAmount ? this.model.overdueAmount.min : ""
    params.partCollectionRemainingDaysMax = params.partCollectionRemainingDays ? this.model.partCollectionRemainingDays.max : ""
    params.partCollectionRemainingDaysMin = params.partCollectionRemainingDays ? this.model.partCollectionRemainingDays.min : ""
    params.personalCollectionRemainingDaysMax = params.personalCollectionRemainingDays ? this.model.personalCollectionRemainingDays.max : ""
    params.personalCollectionRemainingDaysMin = params.personalCollectionRemainingDays ? this.model.personalCollectionRemainingDays.min : ""
    params.followInTimeMin = this.model.followInTime.length ? this.$filter.dateTimeFormat(this.model.followInTime[0]) : ''
    params.followInTimeMax = this.model.followInTime.length ? this.$filter.dateTimeFormat(this.model.followInTime[1]) : ''
    params.loanDateMin = this.$filter.dateRanageFormat(this.model.loanDate).start
    params.loanDateMax = this.$filter.dateRanageFormat(this.model.loanDate).end

    delete params.overdueAmount
    delete params.partCollectionRemainingDays
    delete params.personalCollectionRemainingDays
    delete params.followInTime
    delete params.loanDate

    return params
  }

  /**
   * 查询统计信息
   */
  private queryTotalInfo() {
    this.legallyCaseManageServiceTestD.personalCasePoolAmount(this.paramsHandler()).subscribe(data => this.totalInfo = data)
  }

  /**
   * 查询翻页信息
   */
  private queryPageInfo() {
    this.legallyCaseManageServiceTestD
      .personalCasePool(this.paramsHandler(), this.pageService, this.sortService)
      .subscribe(data => {
        this.dataSet = data
        this.selectionList = []
      });
  }

  private refreshData() {
    this.queryTotalInfo()
    this.queryPageInfo()
  }

  /**
   * 表单范围输入框重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: "", max: "" };
    this.model.partCollectionRemainingDays = { min: '', max: '' }
    this.model.personalCollectionRemainingDays = { min: '', max: "" }
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Legal_personal,
      otherData: {
        data: this.model,
        page: this.pageService,
        sort: this.sortService
      }
    });
  }

  /**
   * 申请流转
   */
  private advanceCirculation() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要流转的案件");
      return;
    }
    this.dialog.apply = true;
  }

  private get selectedCaseIds() {
    return this.selectionList.map(v => v.id);
  }

  private onColorChange(val) {
    const colorModel: any = {
      caseIds: this.selectionList.map(v => v.id),
      caseMark: val,
    };
    this.caseInfoService.caseInfoMarkColor(colorModel).subscribe(this.refreshData);
  }
}
</script>

<style lang="less" scoped>
</style>
